<template>
  <div class="login-wrap">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="form">
      <h4>注册</h4>
      <el-form-item prop="username">
        <el-input
          type="text"
          prefix-icon="el-icon-user"
          placeholder="请输入用户名"
          v-model="ruleForm.username"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          prefix-icon="el-icon-lock"
          v-model="ruleForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password2">
        <el-input
          type="password"
          prefix-icon="el-icon-lock"
          v-model="ruleForm.password2"
          placeholder="请再次输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="nick">
        <el-input
          type="text"
          prefix-icon="el-icon-edit-outline"
          v-model="ruleForm.nick"
          placeholder="请输入昵称"
        ></el-input>
      </el-form-item>
      <el-form-item
        >请选择性别：
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :label="0">秘密</el-radio>
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-cascader
          v-model="location_a"
          :options="options"
          :props="{ expandTrigger: 'hover' }"
          @change="locationChange"
          clearable
        ></el-cascader>
      </el-form-item>
      <div class="btns">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button type="success" plain @click="resetForm('ruleForm')"
          >重置</el-button
        >
        <el-button type="success" plain @click="gotoLogin()">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validatePassword2 = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
        password2: "",
        sex: 0,
        location: "",
        nick: "",
      },
      rules: {
        username: [
          { required: true, message: "必须填写用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "必须填写密码", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "必须填写密码", trigger: "blur" },
          { validator: validatePassword2, trigger: "blur" },
        ],
        nick: [{ required: true, message: "必须填写昵称", trigger: "blur" }],
      },
      location_a: [],
      options: [
        {
          value: "山西省",
          label: "山西省",
          children: [
            {
              value: "太原市",
              label: "太原市",
              children: [{ value: "小店区", label: "小店区" }],
            },
            {
              value: "晋中市",
              label: "晋中市",
              children: [
                {
                  value: "榆次区",
                  label: "榆次区",
                },
                {
                  value: "榆社县",
                  label: "榆社县",
                },
              ],
            },
          ],
        },
        {
          value: "北京市",
          label: "北京市",
        },
        {
          value: "上海",
          label: "上海",
        },
        {
          value: "广州",
          label: "广州",
        },
        {
          value: "深圳",
          label: "深圳",
        },
      ],
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      console.log(this.ruleForm);
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 执行注册
          const res = await this.$http.post("/user/register", this.ruleForm);
          console.log(res);
          if (res.data.code === 0) {
            // 注册成功
            this.$message.success("注册成功");
            localStorage.setItem("token", "Bearer " + res.data.token);
            this.$router.replace("/login");
          } else if (res.data.code === 1) {
            this.$message.error("用户已存在");
          } else {
            this.$message.error("注册失败");
          }
        } else {
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm.sex = 0;
      this.location_a = [];
    },
    // 跳转到登录界面
    gotoLogin() {
      this.$router.replace("/login");
    },
    //选择地区改变事件
    locationChange() {
      this.ruleForm.location = this.location_a.join(" ");
    },
  },
};
</script>
<style lang="less" scoped>
.login-wrap {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    padding: 20px 30px;
    width: 300px;
    height: 450px;
    border: 1px solid #ccc;
    border-radius: 10px;
    h4 {
      margin-bottom: 10px;
      text-align: center;
    }
    .btns {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>